﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Runtime.Serialization;
using System.Collections.ObjectModel;
using System.Text;
using Microsoft.Phone.Shell;

namespace unifiqAnthony
{
    public partial class Page1 : PhoneApplicationPage
    public partial class MainPage : PhoneApplicationPage
    {
        private List<User> userList;
        WebClient client;

        public Page1()
        public MainPage()
        {
            InitializeComponent();
            Loaded += MainPage_Loaded;

        }

        private void MainPage_Loaded(object sender, System.Windows.RoutedEventArgs e)
        private void ListBoxItem_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            string url = "http://unifiq.5kportal.com/retrieve_all_users.php";
            Uri uri = new Uri(url);
            client = new WebClient();
            client.AllowReadStreamBuffering = true;

            client.DownloadStringCompleted += client_DownloadStringCompleted;
            client.DownloadProgressChanged += client_DownloadProgressChanged;

            client.DownloadStringAsync(uri);
            NavigationService.Navigate(new Uri("/Friends.xaml", UriKind.Relative));
        }

        private void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        private void ListBoxItem_Tap_1(object sender, System.Windows.Input.GestureEventArgs e)
        {
            if (e.Cancelled == false & e.Error == null)
            {
                MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(e.Result));
                ObservableCollection<User> list = new ObservableCollection<User>();
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(ObservableCollection<User>));
                list = (ObservableCollection<User>)serializer.ReadObject(ms);

                userList = new List<User>();

                foreach (User user in list)
                {
                    userList.Add(user);
                }
            }

            NavigationService.Navigate(new Uri("/ListingMain.xaml", UriKind.Relative));
        
        }

        private void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        private void ListBoxItem_Tap_2(object sender, System.Windows.Input.GestureEventArgs e)
        {

            NavigationService.Navigate(new Uri("/Planner.xaml", UriKind.Relative));
        }
        private void login_btn_Click(object sender, RoutedEventArgs e)

        private void ListBoxItem_Tap_3(object sender, System.Windows.Input.GestureEventArgs e)
        {
            if (!username_tb.Text.Equals("Enter username") && !password_box.Password.Equals("Password"))
            {
                for (int i = 0; i < userList.Count; i++)
                {
                    if (username_tb.Text.Equals(userList[i].name) && username_tb.Text.Equals(password_box.Password))
                    {
                        Uri targetPage = new Uri("/NavigationPage.xaml", UriKind.Relative);
                        PhoneApplicationService.Current.State["currentUser"] = userList[i];
                        NavigationService.Navigate(targetPage);
                    }
                }
            }
        }



































        private void password_box_MouseEnter(object sender, MouseEventArgs e)
        {
            if (password_box.Password.Equals("Password"))
            {
                password_box.Password = "";
            }
            NavigationService.Navigate(new Uri("/ViewEvent.xaml", UriKind.Relative));
        
        }

        private void password_box_MouseLeave(object sender, MouseEventArgs e)
        private void ListBoxItem_Tap_4(object sender, System.Windows.Input.GestureEventArgs e)
        {
            if (password_box.Password.Equals(""))
            {
                password_box.Password = "Password";
            }
        }

        private void username_tb_MouseEnter(object sender, MouseEventArgs e)
        {
            if (username_tb.Text.Equals("Enter username"))
            {
                username_tb.Text = "";
            }
            NavigationService.Navigate(new Uri("/PivotPage1.xaml", UriKind.Relative));
        
        }

        private void username_tb_MouseLeave(object sender, MouseEventArgs e)
        {
            if (username_tb.Text.Equals(""))
            {
                username_tb.Text = "Enter username";
            }
        }
    }
}